/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.model;

import java.nio.IntBuffer;
import javax.media.opengl.GL3;
import javax.media.opengl.GL3bc;

/**
 *
 * @author Pierre
 */
public class MeshVertexArrayDrawElement {

    final int vertexArrayId;
    final int primitiveType;
    final int numberOfElement;
    final int indexType;

    public MeshVertexArrayDrawElement(int vertexArrayId, int primitiveType, int numberOfElement, int indexType) {
        this.vertexArrayId = vertexArrayId;
        this.primitiveType = primitiveType;
        this.numberOfElement = numberOfElement;
        this.indexType = indexType;
    }

    public void draw(GL3bc gl) {
        //Enable element vertex_buffer_object, don't know why
        gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 1);
        gl.glBindVertexArray(vertexArrayId);

      //  debug(gl);
        gl.glDrawElements(primitiveType, numberOfElement, indexType, 0);
        gl.glBindVertexArray(0);
    }

    private void debug(GL3bc gl) {

        final IntBuffer ib = IntBuffer.wrap(new int[1]);
        for (int loc = 0; loc < 16; loc++) {
            System.out.print("Attribute ");
            System.out.print(loc);


            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_ENABLED, ib);
            System.out.print(" Enabeled:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_SIZE, ib);
            System.out.print(" Size:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_STRIDE, ib);
            System.out.print(" Stride:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_TYPE, ib);
            System.out.print(" Type:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, ib);
            System.out.print(" Normalized:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_INTEGER, ib);
            System.out.print(" Integer:");
            System.out.print(ib.get());
            ib.rewind();
            gl.glGetVertexAttribiv(loc, gl.GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, ib);
            System.out.print(" Buffer:");
            System.out.print(ib.get());
            ib.rewind();

            System.out.println();
        }
        gl.glGetIntegerv(gl.GL_ELEMENT_ARRAY_BUFFER_BINDING, ib);
        System.out.print("Element Buffer:");
        System.out.print(ib.get());
        ib.rewind();
    }

    public void dispose(GL3bc gl) {
        gl.glDeleteVertexArrays(1, new int[]{vertexArrayId}, 0);
    }
}
